package com.rzico.basics.amap.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rzico.basics.amap.pojo.GaodeLocation;
import com.rzico.basics.amap.pojo.Geocodes;
import com.rzico.basics.amap.pojo.regeo.*;
import com.rzico.basics.amap.pojo.request.GeoCodeRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * 地理/逆地理编码
 * <p>
 * Created by lam on 2018/8/8.
 */
public class GaoDeMapUtil {

    // 地理编码地址
    private static String map_geo_url = "http://restapi.amap.com/v3/geocode/geo?parameters";

    // 逆地理编码
    private static String map_regeo_url = "https://restapi.amap.com/v3/geocode/regeo?parameters";

    /**
     * 输入地址返回识别后的信息
     *
     * @param
     * @return 返回的类gaodelocation，详见类
     */
    public GaodeLocation geoCode(GeoCodeRequest request) {
        GaodeLocation location = null;

        if (request == null) {
            location = new GaodeLocation();
            location.setError(true);
            location.setInfo("输入对参数为空");
            return location;
        }

        if (request.getKey() == null || "".equals(request.getKey())) {
            location = new GaodeLocation();
            location.setError(true);
            location.setInfo("请求的服务标识权限为空");
            return location;
        }

        if (request.getAddress() == null || "".equals(request.getAddress())) {
            location = new GaodeLocation();
            location.setError(true);
            location.setInfo("需要查询对地址为空");
            return location;
        }

        try {
            location = new GaodeLocation();
            String result = OKHttpUtil.httpPost(map_geo_url.replace("parameters", ""), request.geoToString());
            System.out.println(result);
            JSONObject jsonObject = JSONObject.parseObject(result);
            // 解析json
            location.setStatus(jsonObject.getString("status"));
            location.setInfo(jsonObject.getString("info"));
            location.setCount(jsonObject.getString("count"));
            List<Geocodes> geocodes = new ArrayList<>();
            JSONArray jsonArray = jsonObject.getJSONArray("geocodes");
            // 遍历解析出来的结果
            if ((jsonArray != null) && (jsonArray.size() > 0)) {
                JSONObject jo = (JSONObject) jsonArray.get(0);
                Geocodes go = new Geocodes();
                go.setFormatted_address(jo.getString("formatted_address"));
                go.setProvince(jo.getString("province"));
                go.setCitycode(jo.getString("citycode"));
                go.setCity(jo.getString("city"));
                go.setDistrict(jo.getString("district"));
                // 地址所在的乡镇
                JSONArray ts = jo.getJSONArray("township");
                if (ts != null && ts.size() > 0) {
                    go.setTownship(ts.getString(0));
                }
                // 地址编号
                go.setAdcode(jo.getString("adcode"));
                // 街道
                JSONArray jd = jo.getJSONArray("street");
                if (jd != null && jd.size() > 0) {
                    go.setStreet(jd.getString(0));
                }
                // 号码
                JSONArray hm = jo.getJSONArray("number");
                if (hm != null && hm.size() > 0) {
                    go.setStreet(hm.getString(0));
                }
                go.setLocation(jo.getString("location"));
                go.setLevel(jo.getString("level"));
                geocodes.add(go);
            }
            location.setGeocodes(geocodes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return location;
    }

    public GaodeLocation getReGeoodes(GeoCodeRequest request) {
        GaodeLocation location = new GaodeLocation();

        if (request == null) {
            location = new GaodeLocation();
            location.setError(true);
            location.setInfo("输入对参数为空");
            return location;
        }

        if (request.getKey() == null || "".equals(request.getKey())) {
            location = new GaodeLocation();
            location.setError(true);
            location.setInfo("请求的服务标识权限为空");
            return location;
        }

        if (request.getLocation() == null || "".equals(request.getLocation())) {
            location = new GaodeLocation();
            location.setError(true);
            location.setInfo("需要查询的经纬度为空");
            return location;
        }

        try {
            String result = OKHttpUtil.httpPost(map_regeo_url.replace("parameters", ""), request.reGeoToString());
            System.out.println(result);

            JSONObject jsonObject = JSONObject.parseObject(result);
            // 解析json
            location.setStatus(jsonObject.getString("status"));
            location.setInfo(jsonObject.getString("info"));
            location.setCount(jsonObject.getString("count"));
            ReGeoCode go = new ReGeoCode();
            JSONObject jo = jsonObject.getJSONObject("regeocode");

            go.setFormatted_address(jo.getString("formatted_address"));
            AddressComponent addressComponent = new AddressComponent();
            go.setAddressComponent(addressComponent);
            JSONObject adr = jo.getJSONObject("addressComponent");
            addressComponent.setProvince(adr.getString("province"));
            addressComponent.setCitycode(adr.getString("citycode"));
            addressComponent.setCity(adr.getString("city"));
            addressComponent.setDistrict(adr.getString("district"));
            addressComponent.setAdcode(adr.getString("adcode"));
            addressComponent.setTowncode(adr.getString("towncode"));
            addressComponent.setTownship(adr.getString("township"));
            Building building = new Building();
            addressComponent.setBuilding(building);
            JSONObject bd = adr.getJSONObject("building");
            if (bd!=null) {
                building.setName(bd.getString("name"));
                building.setType(bd.getString("type"));
            }
            StreetNumber streetNumber = new StreetNumber();
            addressComponent.setStreetNumber(streetNumber);
            JSONObject sn = adr.getJSONObject("streetNumber");
            streetNumber.setStreet(sn.getString("street"));
            streetNumber.setNumber(sn.getString("number"));
            streetNumber.setLocation(sn.getString("location"));
            streetNumber.setDirection(sn.getString("direction"));
            streetNumber.setDistance(sn.getString("distance"));

            JSONArray jsonArray = jo.getJSONArray("pois");
            // 遍历解析出来的结果
            if ((jsonArray != null) && (jsonArray.size() > 0)) {
                List<Poi> pois = new ArrayList<>();
                go.setPois(pois);
                for (int i=0;i<jsonArray.size();i++) {
                    JSONObject po = jsonArray.getJSONObject(i);
                    if (i==0) {
                        building.setName(po.getString("name"));
                        building.setType(po.getString("type"));
                    }
                    Poi poi = new Poi();
                    poi.setAdCode(addressComponent.getAdcode());
                    if (addressComponent.getProvince().equals(addressComponent.getCity())) {
                        poi.setAdName(addressComponent.getCity().concat(addressComponent.getDistrict()));
                    } else {
                        poi.setAdName(addressComponent.getProvince().concat(addressComponent.getCity()).concat(addressComponent.getDistrict()));
                    }
                    poi.setAddress(po.getString("address"));
                    poi.setDirection(po.getString("direction"));
                    poi.setId(po.getString("id"));
                    poi.setLocation(po.getString("location"));
                    poi.setName(po.getString("name"));
                    poi.setType(po.getString("type"));
                    poi.setBusinessarea(po.getString("businessarea"));
                    poi.setDistance(po.getString("distance"));
                    pois.add(poi);
                }
            }
            location.setRegeocode(go);
            location.setError(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return location;
    }

}
